package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingCadetTeacherExperience;
import com.ruoyi.system.domain.TrainingCadetTeacherExperienceCount;
import com.ruoyi.system.mapper.TrainingCadetTeacherExperienceMapper;
import com.ruoyi.system.service.ITrainingCadetTeacherExperienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 跟师心得Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingCadetTeacherExperienceServiceImpl implements ITrainingCadetTeacherExperienceService {
    @Autowired
    private TrainingCadetTeacherExperienceMapper trainingCadetTeacherExperienceMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询跟师心得
     *
     * @param experienceId 跟师心得主键
     * @return 跟师心得
     */
    @Override
    public TrainingCadetTeacherExperience selectTrainingCadetTeacherExperienceByExperienceId(Long experienceId) {
        return trainingCadetTeacherExperienceMapper.selectTrainingCadetTeacherExperienceByExperienceId(experienceId);
    }

    /**
     * 查询跟师心得列表
     *
     * @param trainingCadetTeacherExperience 跟师心得
     * @return 跟师心得
     */
    @Override
    public List<TrainingCadetTeacherExperience> selectTrainingCadetTeacherExperienceList(TrainingCadetTeacherExperience trainingCadetTeacherExperience) {
        return trainingCadetTeacherExperienceMapper.selectTrainingCadetTeacherExperienceList(trainingCadetTeacherExperience);
    }

    /**
     * 查询跟师心得合计列表
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param experienceStatus
     * @return
     */
    @Override
    public List<TrainingCadetTeacherExperienceCount> selectTrainingCadetTeacherExperienceCount(
            String submitBegin, String submitEnd, String baseId, String experienceStatus, Long cadetId, String cadetName) {
        return trainingCadetTeacherExperienceMapper.selectTrainingCadetTeacherExperienceCount(
                submitBegin, submitEnd, baseId, experienceStatus, cadetId, cadetName);
    }

    @Override
    public List<TrainingCadetTeacherExperience> getExperienceList(String submitBegin, String submitEnd, String baseId, String cadetId, String experienceStatus) {
        return trainingCadetTeacherExperienceMapper.getExperienceList(submitBegin, submitEnd, baseId, cadetId, experienceStatus);
    }

    /**
     * 新增跟师心得
     *
     * @param trainingCadetTeacherExperience 跟师心得
     * @return 结果
     */
    @Override
    public int insertTrainingCadetTeacherExperience(TrainingCadetTeacherExperience trainingCadetTeacherExperience) {
        trainingCadetTeacherExperience.setCreateTime(DateUtils.getNowDate());
        if (trainingCadetTeacherExperience.getMentorId() != null && trainingCadetTeacherExperience.getMentorId().isEmpty()) {
            trainingCadetTeacherExperience.setMentorId(null);
        }
        return trainingCadetTeacherExperienceMapper.insertTrainingCadetTeacherExperience(trainingCadetTeacherExperience);
    }

    /**
     * 修改跟师心得
     *
     * @param trainingCadetTeacherExperience 跟师心得
     * @return 结果
     */
    @Override
    public int updateTrainingCadetTeacherExperience(TrainingCadetTeacherExperience trainingCadetTeacherExperience) {
        if (trainingCadetTeacherExperience.getMentorId() != null && trainingCadetTeacherExperience.getMentorId().isEmpty()) {
            trainingCadetTeacherExperience.setMentorId(null);
        }
        return trainingCadetTeacherExperienceMapper.updateTrainingCadetTeacherExperience(trainingCadetTeacherExperience);
    }

    /**
     * 批量删除跟师心得
     *
     * @param experienceIds 需要删除的跟师心得主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetTeacherExperienceByExperienceIds(Long[] experienceIds) {
        return trainingCadetTeacherExperienceMapper.deleteTrainingCadetTeacherExperienceByExperienceIds(experienceIds);
    }

    /**
     * 删除跟师心得信息
     *
     * @param experienceId 跟师心得主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetTeacherExperienceByExperienceId(Long experienceId) {
        return trainingCadetTeacherExperienceMapper.deleteTrainingCadetTeacherExperienceByExperienceId(experienceId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingCadetTeacherExperience 实体
     * @param userId                         用户id
     * @param crateType                      创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingCadetTeacherExperience trainingCadetTeacherExperience, Long userId, Integer crateType) {
        //主键
        if (trainingCadetTeacherExperience.getExperienceId() == null || "".equals(trainingCadetTeacherExperience.getExperienceId())) {
            trainingCadetTeacherExperience.setExperienceId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingCadetTeacherExperience.getCreateBy() == null) {
                trainingCadetTeacherExperience.setCreateBy(userId.toString());
            }
            if (trainingCadetTeacherExperience.getCreateTime() == null) {
                trainingCadetTeacherExperience.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingCadetTeacherExperience.getModifyBy() == null) {
            trainingCadetTeacherExperience.setModifyBy(userId);
        }
        //修改时间
        if (trainingCadetTeacherExperience.getModifyTime() == null) {
            trainingCadetTeacherExperience.setModifyTime(DateUtils.getNowDate());
        }
    }
}
